//
//  main.m
//  OC06_集合遍历_数组排序
//
//  Created by Scott on 16/3/2.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

/** 知识点: for 循环遍历. */
void chapterOfFor() {
    
#pragma mark 1.1 数组遍历.
    
#if 0
    
    /**
     * 练习:
     *
     * 使用for循环完成:
     * 定义一个数组, 包含5个字符串对象, 倒序取出数组中所有元素, 并存储到另一可变数组中。
     */
    NSArray *forArray = @[@"sichangbao",@"laochen",@"laozhou",@"laoyu",@"hensha"];
    NSMutableArray *forMArray = [NSMutableArray arrayWithCapacity:5];
    for (NSInteger i = forArray.count - 1; i >=0; i--) {
        NSLog(@"%@",[forArray objectAtIndex:i]);
        [forMArray addObject:[forArray objectAtIndex:i]];
    }
    NSLog(@"%@",forMArray);
    
#endif
#pragma mark 1.2 字典遍历.
    
    
    
    
    /**
     * 练习:
     *
     * 使用for循环完成:
     * 定义一个字典, 用于存储一个人的name, sex, age三个数据, 完成对其三个数据的输出。
     */
#if 0
    NSMutableDictionary *dictOfPerson = [@{@"name":@"sichangbao",@"sex":@"M",@"age":@"16"} mutableCopy];
    
    NSArray *arr = dictOfPerson.allKeys;
    for(int i = 0;i < arr.count;i++){
        NSLog(@"%@:%@",[arr objectAtIndex:i],[dictOfPerson objectForKey:[arr objectAtIndex:i]]);
    }
    
#endif
    
#pragma mark 1.3 集合遍历.
#if 0
    NSSet *set1 = [NSSet setWithObjects:@"a",@"b",@"c", nil];
    NSArray *arr1 = set1.allObjects;
    for (int i = 0; i < arr1.count; i++) {
        NSLog(@"%@",[arr1 objectAtIndex:i]);
    }
    

#endif
}
/** 知识点: NSEnumerator(枚举器) 遍历. */
void chapterOfNSEnumerator() {
#if 0
#pragma mark 2.1 数组正序枚举.
    NSArray *array = @[@"element1", @"element2", @"element3", @"element4"];
    NSEnumerator *enumer = [array objectEnumerator];
    id anObject;
    while (anObject = [enumer nextObject]) {
        [anObject class];
        NSLog(@"%@",anObject);
    }
    
#pragma mark 2.2 数组反向枚举.
    NSEnumerator *reverseEnumer = [array reverseObjectEnumerator];
    id secondObject;
    while (secondObject = [reverseEnumer nextObject]) {
        NSLog(@"%@",secondObject);
    }
#pragma mark 2.3 字典枚举.
    NSDictionary *dict = @{@"0":@"ni",@"1":@"wo",@"2":@"ta"};
 //   NSEnumerator *dictKeyEnumer = [dict keyEnumerator];
    NSEnumerator *dictObjectEnumer = [dict objectEnumerator];
    id dictOfObject;
    while (dictOfObject =  [dictObjectEnumer nextObject]) {
        NSLog(@"%@",dictOfObject);
    }
    
    
#pragma mark 2.4 集合枚举.
    NSSet *set = [NSSet setWithObjects:@"see",@"you",@"again", nil];
    NSEnumerator *setEnumer = [set objectEnumerator];
    id setOfObject;
    while (setOfObject = [setEnumer nextObject]){
        NSLog(@"%@",setOfObject);
    }
    
    
#endif
    
}

/** 知识点: for...in 遍历. */  //快速遍历
void chapterOfForin() {
#pragma mark 3.1 数组遍历.
#if 0
//    NSArray *array = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];
//    for(NSString *str in array){
//        NSLog(@"%@",str);
//    }
    
    /**
     * 练习:
     *
     * 使用 for...in 完成:
     * 定义一个数组, 包含5个字符串对象, 取出数组中所有字符串, 并拼接生成一个新的字符串。
     */
    NSArray *findBearArray = [NSArray arrayWithObjects:@"Do",@" you",@" see",@" my",@" bear",nil];
    NSMutableString *strForReturn = [[NSMutableString alloc] initWithCapacity:5];
    for(NSString *str2 in findBearArray){
        [strForReturn appendString:str2];
    }
    NSLog(@"%@",strForReturn);
    
#pragma mark 3.2 字典遍历.
    
    
    
    /**
     * 练习:
     *
     * 使用 for...in... 完成:
     * 定义一个字典, 用于存储一个人的name, sex, age三个数据,  for...in...遍历, 输出此人的name到控制台
     */
    
    NSDictionary *dictByForIn = [NSDictionary dictionaryWithObjectsAndKeys:@"sichangbao",@"name",@"M",@"sex",@"18",@"age", nil];
    for (NSString *key in dictByForIn){
        if([key isEqualToString:@"name"]){
            NSLog(@"%@",[dictByForIn objectForKey:key]);
        }
    }
    
    
#pragma mark 3.3 集合遍历.
    NSSet *setForIn = [NSSet setWithObjects:@"mianbao",@"niunai",@"xiangchang",nil];
    for (NSString *strForIn in setForIn){
        NSLog(@"%@",strForIn);
    }
    
#endif

//枚举遍历和快速遍历:不可以在遍历的过程中对集合里的元素做增加或者删除等操作



//NSMutableArray *mutableArray =
//  @[@"zhaowuyi",@"tianshun",@"wanghuaqian",@"lijie"].mutableCopy;
//for (NSString *str in mutableArray){
//    if([str isEqualToString:@"zhaowuyi"]){
//        [mutableArray removeObject:str];
//    }
//}
 
    
    
}
/** 知识点: 数组排序. */
void chapterOfSort() {
    
#pragma mark 4.1 NSSortDescriptor
    
    //不可变数组的排序方法
    NSArray *array =@[@"zhaowuyi",@"lijie",@"liyang",@"tianshun",@"chengcheng"];
    //创建排序描述类的对象
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES];
    //对数组进行排序,得到新的排序好的数组
    NSArray *sortArray = [array sortedArrayUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@",sortArray);
    
    /** 可变数组的排序方法 */
    NSMutableArray *mArry = @[@"ok",@"hello",@"nihao",@"what"].mutableCopy;
    NSSortDescriptor *sortDescripMArry = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
    [mArry sortUsingDescriptors:@[sortDescripMArry]];
    NSLog(@"%@",mArry);
    
    
    
#pragma mark 4.2 selector
    //不可变数组的排序方法
    NSArray *array2 = @[@"zhaoshahuo",@"lihe",@"gaozhi",@"chengcheng"];
    //参数类型SEL: @selector(方法名)
    NSArray *newArray = [array2 sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@",newArray);
    
    NSMutableArray *arrayM = [NSMutableArray arrayWithObjects:@"sichangbao",@"liyang",@"lijie",@"gaozhipeng", nil];
    [arrayM sortUsingSelector:@selector(compare:)];
    NSLog(@"%@",arrayM);
    
    
    
    /**
     * 练习:
     *
     * 生成一个包含5个Person对象的可变数组
    
     使用sortedArrayUsingSelector: 按姓名降序排序
     使用sortUsingSelector: 按年龄升序排序
     */
    Person *per1 = [[Person alloc] initWithName:@"sichangbao" withAge:25 withSex:@"M"];
    Person *per2 = [[Person alloc] initWithName:@"baohei" withAge:22 withSex:@"M"];
    Person *per3 = [[Person alloc] initWithName:@"sichangbao" withAge:29 withSex:@"M"];
    
    NSMutableArray *mArrayPerson = [NSMutableArray arrayWithObjects:per1,per2,per3,nil];
    NSSortDescriptor *sortDescNameAscend = [NSSortDescriptor sortDescriptorWithKey:@"self.name" ascending:YES];
    NSArray *returnArray = [mArrayPerson sortedArrayUsingDescriptors:@[sortDescNameAscend]];
    NSLog(@"%@",returnArray);
    
    
    NSSortDescriptor *sortDescAgeDscend =[NSSortDescriptor sortDescriptorWithKey:@"self.age" ascending:NO];
    [mArrayPerson sortUsingDescriptors:@[sortDescNameAscend,sortDescAgeDscend]];
    NSLog(@"%@",mArrayPerson);
    
    /**自定义类的排序方法*/
    
    NSArray *arrayOfPers = @[per1,per2,per3];
    
    //排序条件具体是由参数方法所决定的.
    NSArray *newArrayOfPers = [arrayOfPers sortedArrayUsingSelector:@selector(compareWithName:)];
    
    
    //按照年龄进行升序排序
    NSArray *newArrayOfPersByAge = [arrayOfPers sortedArrayUsingSelector:@selector(compareWithAge:)];
    NSLog(@"%@",newArrayOfPersByAge);
    
    
    
}



#pragma mark - MAIN FUNCTION
int main(int argc, const char * argv[]) {
    
#pragma mark - 知识点1. for 循环遍历.
    chapterOfFor();
    
#pragma mark - 知识点2. NSEnumerator(枚举器) 遍历.
    chapterOfNSEnumerator();
    
#pragma mark - 知识点3. for...in 遍历.
    chapterOfForin();
    
#pragma mark - 知识点4. 数组排序.
    chapterOfSort();
    
    return 0;
}










